<template>
  <a-modal
    :destroy-on-close="true"
    :mask-closable="false"
    :visible="visible"
    :title="title"
    :confirm-loading="loading"
    @cancel="handleClose"
    @ok="handleSubmit"
  >
    <a-form ref="formRef" :model="formData" :rules="formRules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
      <a-form-item label="角色名称" name="roleName">
        <a-input v-model:value="formData.roleName" placeholder="请输入角色名称" />
      </a-form-item>
      <a-form-item label="角色标识" name="roleKey">
        <a-input v-model:value="formData.roleKey" placeholder="请输入角色标识" />
      </a-form-item>
      <a-form-item label="角色描述" name="description">
        <a-textarea v-model:value="formData.description" placeholder="请输入角色描述" :rows="4" />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="formData.status" button-style="solid">
          <a-radio-button v-for="item in AvailableStatusEnum.$options()" :key="item.value" :value="item.value">{{
            item.label
          }}</a-radio-button>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script name="SystemRoleEdit" lang="ts" setup>
import { getRole, saveRole, updateRole } from "@/api/system/role";
import { enum2Value } from "@/utils/conver";
import { AvailableStatusEnum } from "@/enums/common";

const emit = defineEmits(["reload"]);

const title = ref("添加角色");
const loading = ref(false);
const visible = ref(false);

const formRef = ref();
const formData = reactive<any>({
  roleId: undefined
});
const formRules = reactive({
  roleName: [{ required: true, trigger: "blur", message: "请输入角色名称" }],
  roleKey: [{ required: true, trigger: "blur", message: "请输入角色标识" }]
});
const handleGetRole = (id: number) => {
  loading.value = true;
  getRole(id)
    .then(response => {
      Object.assign(formData, enum2Value(response.data));
    })
    .finally(() => {
      loading.value = false;
    });
};

const handleAdd = () => {
  Object.assign(
    formData,
    reactive({
      roleId: undefined,
      roleName: "",
      roleKey: "",
      description: "",
      status: AvailableStatusEnum.ENABLE.code
    })
  );
  title.value = "添加角色";
  visible.value = true;
};

const handleEdit = (id: number) => {
  handleGetRole(id);
  title.value = "编辑角色";
  visible.value = true;
};

const handleClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};

const handleSubmit = () => {
  formRef.value
    .validate()
    .then(() => {
      loading.value = true;
      if (formData.roleId === undefined) {
        saveRole(formData)
          .then(() => {
            antdMessage.success("添加成功");
            emit("reload");
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        updateRole(formData)
          .then(() => {
            antdMessage.success("编辑成功");
            emit("reload");
            handleClose();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    })
    .catch(error => {
      // console.log("error", error);
    });
};

defineExpose({
  handleAdd,
  handleEdit
})
</script>
